'''
Created on Mar 10, 2010

@author: wangyongtao
'''
from google.appengine.ext.webapp import template

def truncatechinese(value, arg):    
    """    
    Truncates a string after a certain number of words including    
    alphanumeric and CJK characters.     
    Argument: Number of words to truncate after.    
    """    
    try:
        bits = []
        for x in arg.split(u':'):
            if len(x) == 0:
                bits.append(None)
            else:
                bits.append(int(x))
        if int(x) < len(value):
            return value[slice(*bits)] + '...'
        return value[slice(*bits)]

    except (ValueError, TypeError):
        return value # Fail silently.
# Register the filter functions with Django
register = template.create_template_register()
register.filter(truncatechinese)
